﻿using System;

// 泛型链表节点
public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }

    // 构造函数
    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

// 泛型链表类
public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList()
    {
        head = tail = null;
    }

    public Node<T> Head
    {
        get => head;
    }

    // 添加元素到链表尾部
    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    // 遍历链表并执行指定操作
    public void ForEach(Action<T> action)
    {
        Node<T> current = head;
        while (current != null)
        {
            action(current.Data);
            current = current.Next;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 创建泛型链表
        GenericList<int> list = new GenericList<int>();

        // 向链表中添加元素
        list.Add(3);
        list.Add(5);
        list.Add(12);
        list.Add(7);
        list.Add(22);

        // 打印链表元素
        Console.WriteLine("链表元素：");
        list.ForEach(x => Console.WriteLine(x));

        // 求链表元素的最大值
        int max = int.MinValue;
        list.ForEach(x => { if (x > max) max = x; });
        Console.WriteLine("最大值：" + max);

        // 求链表元素的最小值
        int min = int.MaxValue;
        list.ForEach(x => { if (x < min) min = x; });
        Console.WriteLine("最小值：" + min);

        // 求链表元素的和
        int sum = 0;
        list.ForEach(x => sum += x);
        Console.WriteLine("总和：" + sum);
    }
}
